//
//  GesturesPasswordViewController.m
//  MyFramework
//
//  Created by 余洋 on 2018/7/25.
//  Copyright © 2018年 余洋. All rights reserved.
//

#import "GesturesPasswordViewController.h"
#import "PasswordView.h"
#import "UIViewController+HUD.h"

@interface GesturesPasswordViewController ()

@property (nonatomic, strong) NSString *firstPassword;
@property (weak, nonatomic) IBOutlet UILabel *messageLabel;
@property (weak, nonatomic) IBOutlet PasswordView *passwordView;

@end

@implementation GesturesPasswordViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    __weak typeof(self) weakSelf = self;
    [self.passwordView setDrawFinishBlock:^(NSString *password) {
        [weakSelf recordPassword:password];
    }];
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    if (self.type == GesturesPasswordUnlock) {
        self.messageLabel.text = @"请解锁手势密码";
    }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}

#pragma mark action
- (IBAction)closeViewAction:(id)sender {
    [self dismissViewControllerAnimated:YES completion:nil];
}

- (void)recordPassword:(NSString *)password {
    if (self.type == GesturesPasswordCreate) {//创建手势密码
        [self checkPassword:password];
    } else {//解锁手势密码
        [self unlockPassword:password];
    }
}

/**
 校验密码,并保存
 */
- (void)checkPassword:(NSString *)password {
    if (self.firstPassword.length > 0) {
        if ([self.firstPassword isEqualToString:password]) {
            [[NSUserDefaults standardUserDefaults] setObject:password forKey:GesturesPassword];
            [[NSUserDefaults standardUserDefaults] synchronize];
            [self dismissViewControllerAnimated:YES completion:nil];
            [self lc_showToastMessageSuccess:@"手势密码保存成功"];
        } else {
            self.messageLabel.text = @"两次不一致，请重新绘制";
            [self shakeAnimationForView:self.messageLabel];
        }
    } else {
        if (password.length > 3) {
            self.firstPassword = password;
            self.messageLabel.text = @"请再次绘制手势密码";
        } else {
            self.messageLabel.text = @"至少连接四个点";
            [self shakeAnimationForView:self.messageLabel];
        }
    }
}

/**
 解锁手势密码
 */
- (void)unlockPassword:(NSString *)password {
    NSString *passwordOld = [[NSUserDefaults standardUserDefaults] objectForKey:GesturesPassword];
    if ([password isEqualToString:passwordOld]) {
        self.type = GesturesPasswordCreate;
        self.messageLabel.text = @"请绘制手势密码";
    } else {
        self.messageLabel.text = @"密码不正确";
        [self shakeAnimationForView:self.messageLabel];
    }
}
/**
 清空手势密码
 
 [NSUserDefaults standardUserDefaults]是系统提供的单例方法。一般通过[[NSUserDefaults standardUserDefaults] setObject:forKey:]来存储数据，通过[[NSUserDefaults standardUserDefaults] objectForKey:]来获取已存数据。每次存储数据后用[[NSUserDefaults standardUserDefaults] synchronize]方法持久化数据，直接同步到文件里。
 */
- (IBAction)cleanGesturePasswordAction:(id)sender {
    [[NSUserDefaults standardUserDefaults] removeObjectForKey:GesturesPassword];
    [[NSUserDefaults standardUserDefaults] synchronize];
    self.messageLabel.text = @"请绘制手势密码";
    self.type = GesturesPasswordCreate;
}

//抖动动画
- (void)shakeAnimationForView:(UIView *)view{
    CALayer *viewLayer = view.layer;
    CGPoint position = viewLayer.position;
    CGPoint left = CGPointMake(position.x - 10, position.y);
    CGPoint right = CGPointMake(position.x + 10, position.y);
    
    CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"position"];
    [animation setTimingFunction:[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut]];
    [animation setFromValue:[NSValue valueWithCGPoint:left]];
    [animation setToValue:[NSValue valueWithCGPoint:right]];
    [animation setAutoreverses:YES]; // 平滑结束
    [animation setDuration:0.08];
    [animation setRepeatCount:3];
    [viewLayer addAnimation:animation forKey:nil];
}

@end
